package test;

import lombok.Data;
import lombok.Getter;

import java.awt.*;
import java.util.*;
import java.util.List;

/**
 * 双色球一些想法
 */
public class SSQ {

    Integer LUCK_NUM = 16;

    //历史中奖号码
    HashMap<Integer, List<Integer>> HISTORY_DATA = new LinkedHashMap<>();

    //当前周期中，距离该号码出现的最大次数
    HashMap<Integer, List<Integer>> ORDINARY_MAX_LAST_APPEAR = new LinkedHashMap<>();
    HashMap<Integer, List<Integer>> LUCK_MAX_LAST_APPEAR = new LinkedHashMap<>();

    /**
     * 随机生成1000个历史号码
     */

    void generateHistory() {
        for (int i = 0; i < 1000; i++) {
            HISTORY_DATA.put(i, generateOneLottery());
        }
    }

    /**
     * 生成一组中奖号码
     *
     * @return
     */
    List<Integer> generateOneLottery() {
        List<Integer> result = new ArrayList<>();
        List<Integer> initOrdinaryNums = getInitOrdinaryNums();
        List<Integer> initLuckOrdinaryNums = getInitLuckOrdinaryNums();
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            int index = random.nextInt(initOrdinaryNums.size());
            Integer choose = initOrdinaryNums.get(index);
            initOrdinaryNums.remove(index);
            result.add(choose);
        }
        result.add(initLuckOrdinaryNums.get(random.nextInt(initLuckOrdinaryNums.size())));

        return result;
    }


    /**
     * 计算当前周期幸运数字位 各个数字多久没出现了
     *
     * @param period
     */
    List<NumLatestNum> caclLuckMaxLastAppear(int period) {
        int lastestPeriod = Math.min(period - 1, HISTORY_DATA.size());
        List<NumLatestNum> result = new ArrayList<>();
        for (int j = 1; j < LUCK_NUM + 1; j++) {
            for (int i = lastestPeriod - 1; i >= 0; i--) {
                if (j == HISTORY_DATA.get(i).get(6)) {
                    result.add(new NumLatestNum(j, lastestPeriod - i));
                    break;
                }
                if (i == 0) {
                    result.add(new NumLatestNum(j, -1));
                }
            }

        }
        return result;
    }

    /**
     * 获取普通数字池
     *
     * @return
     */
    List<Integer> getInitOrdinaryNums() {
        return generateNums(33);
    }


    /**
     * 获取幸运数字池
     *
     * @return
     */
    List<Integer> getInitLuckOrdinaryNums() {
        return generateNums(16);
    }

    /**
     * 生成从1-n的数据
     *
     * @param n
     * @return
     */
    List<Integer> generateNums(Integer n) {
        List<Integer> result = new LinkedList<>();
        for (int i = 0; i < n; i++) {
            result.add(i + 1);
        }

        return result;
    }

    public static void main(String[] args) {
        SSQ ssq = new SSQ();
        //生成一千条数
        ssq.generateHistory();
        int total = 0;
        int fail = 0;
        int oneSuccess=0;
        int twoSuccess = 0;
        int threeSuccess = 0;
        int fourSuccess = 0;
        int fiveSuccess = 0;
        for (int i = 1001; i < 100000; i++) {
            List<NumLatestNum> properLuckNum = ssq.caclLuckMaxLastAppear(i);//猜测的最大数
            properLuckNum.sort(Comparator.comparing(t -> -t.latestNum));

            Integer actualLuckNUm = ssq.generateOneLottery().get(6);
            StringBuilder sb = new StringBuilder();
            if (properLuckNum.get(0).num == actualLuckNUm) {
                oneSuccess++;
                ssq.printInfo(i, actualLuckNUm, "success first");
            }
            else if (properLuckNum.get(1).num == actualLuckNUm) {
                twoSuccess++;
                ssq.printInfo(i, actualLuckNUm, "success second");
            }
            else if (properLuckNum.get(2).num == actualLuckNUm) {
                threeSuccess++;
                ssq.printInfo(i, actualLuckNUm, "success third");
            }
            else if (properLuckNum.get(3).num == actualLuckNUm) {
                fourSuccess++;
                ssq.printInfo(i, actualLuckNUm, "success four");
            }
            else if (properLuckNum.get(4).num == actualLuckNUm) {
                fiveSuccess++;
                ssq.printInfo(i, actualLuckNUm, "success five");
            }
            else {
                System.out.println("fail");
                fail++;
            }
            total++;
        }
        System.out.println("one " +(1.0*oneSuccess)/total);
        System.out.println("two " +(1.0*twoSuccess)/total);
        System.out.println("three " +(1.0*threeSuccess)/total);
        System.out.println("four " +(1.0*fourSuccess)/total);
        System.out.println("five " +(1.0*fiveSuccess)/total);
        System.out.println(fail*1.0/total);
        ssq.caclLuckMaxLastAppear(1001);
        System.out.println(ssq);
    }

    void printInfo(int i, int actualNum, String infoTag) {

        System.out.println((infoTag + "  " + i + " actual " + actualNum));
    }


}

/**
 * 数字和最近出现的相差周期数
 */
@Data
@Getter
class NumLatestNum {
    Integer num;
    Integer latestNum;

    public NumLatestNum(Integer num, Integer latestNum) {
        this.num = num;
        this.latestNum = latestNum;
    }
}
